package com.ruoyi.training.service.impl;

import java.util.List;
import java.util.StringJoiner;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.training.domain.PaymentRecord;
import com.ruoyi.training.domain.TrainingClass;
import com.ruoyi.training.dto.payment.InvoiceRefundDto;
import com.ruoyi.training.dto.registration.RegistrationDto;
import com.ruoyi.training.mapper.PaymentRecordMapper;
import com.ruoyi.training.mapper.RegistrationMapper;
import com.ruoyi.training.mapper.TrainingClassMapper;
import com.ruoyi.training.util.xuehua.SnowflakeIdUtils;
import com.ruoyi.training.vo.payment.ChangeInvoiceVo;
import com.ruoyi.training.vo.payment.InvoiceRefundVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.training.mapper.InvoiceMapper;
import com.ruoyi.training.domain.Invoice;
import com.ruoyi.training.service.IInvoiceService;

/**
 * 发票信息Service业务层处理
 *
 * @author hcc
 * @date 2025-09-29
 */
@Service
public class InvoiceServiceImpl implements IInvoiceService
{
    @Autowired
    private InvoiceMapper invoiceMapper;

    @Autowired
    private PaymentRecordMapper paymentRecordMapper;

    @Autowired
    private RegistrationMapper registrationMapper;

    @Autowired
    private TrainingClassMapper trainingClassMapper;
    /**
     * 查询发票信息
     *
     * @param invoiceId 发票信息主键
     * @return 发票信息
     */
    @Override
    public Invoice selectInvoiceByInvoiceId(String invoiceId)
    {
        return invoiceMapper.selectInvoiceByInvoiceId(invoiceId);
    }

    /**
     * 查询发票信息列表
     *
     * @param invoice 发票信息
     * @return 发票信息
     */
    @Override
    public List<Invoice> selectInvoiceList(Invoice invoice)
    {
        return invoiceMapper.selectInvoiceList(invoice);
    }

    /**
     * 新增发票信息
     *
     * @param invoice 发票信息
     * @return 结果
     */
    @Override
    public int insertInvoice(Invoice invoice)
    {
        invoice.setInvoiceId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
        invoice.setCreateTime(DateUtils.getNowDate());
        return invoiceMapper.insertInvoice(invoice);
    }

    /**
     * 修改发票信息
     *
     * @param invoice 发票信息
     * @return 结果
     */
    @Override
    public int updateInvoice(Invoice invoice)
    {
        invoice.setInvoiceRefundTime(DateUtils.getNowDate());
        invoice.setInvoiceRefundPerson(SecurityUtils.getUsername());
        invoice.setUpdateTime(DateUtils.getNowDate());
        return invoiceMapper.updateInvoice(invoice);
    }

    /**
     * 批量删除发票信息
     *
     * @param invoiceIds 需要删除的发票信息主键
     * @return 结果
     */
    @Override
    public int deleteInvoiceByInvoiceIds(String[] invoiceIds)
    {
        return invoiceMapper.deleteInvoiceByInvoiceIds(invoiceIds);
    }

    /**
     * 删除发票信息信息
     *
     * @param invoiceId 发票信息主键
     * @return 结果
     */
    @Override
    public int deleteInvoiceByInvoiceId(String invoiceId)
    {
        return invoiceMapper.deleteInvoiceByInvoiceId(invoiceId);
    }

    /**
     * 获取退换票申请信息
     */
    @Override
    public InvoiceRefundDto getInvoiceRefundInfo(InvoiceRefundVo invoiceRefundVo) {
        InvoiceRefundDto invoiceRefundDto = new InvoiceRefundDto();
        if (StringUtils.isNotEmpty(invoiceRefundVo.getInvoiceId())) {
            Invoice invoice = invoiceMapper.selectInvoiceByInvoiceId(invoiceRefundVo.getInvoiceId());
            if (StringUtils.isNotNull(invoice)){
                BeanUtils.copyProperties(invoice,invoiceRefundDto);
            }
        }
        if (StringUtils.isNotEmpty(invoiceRefundVo.getRegistrationId())){
            RegistrationDto registrationDto = registrationMapper.selectRegistrationByRegistrationId(invoiceRefundVo.getRegistrationId());
            if (StringUtils.isNotNull(registrationDto)){
                BeanUtils.copyProperties(registrationDto,invoiceRefundDto);
            }
        }
        return invoiceRefundDto;
    }

    /**
     * 批量修改发票状态
     */
    @Override
    public int changeInvoiceStatus(ChangeInvoiceVo changeInvoiceVo) {
        String[] invoiceIds = changeInvoiceVo.getInvoiceIds();
        int result = 0;
        for (String invoiceId : invoiceIds) {
            Invoice invoice = invoiceMapper.selectInvoiceByInvoiceId(invoiceId);
            if (StringUtils.isNotNull(invoice)){
                invoice.setInvoiceStatus(changeInvoiceVo.getInvoiceStatus());
                invoice.setUpdateTime(DateUtils.getNowDate());
                invoice.setUpdateBy(SecurityUtils.getUsername());
                result = invoiceMapper.updateInvoice(invoice);
            }
        }
        return result;
    }

    /**
     * 获取退换票申请信息
     */
    @Override
    public InvoiceRefundDto getInvoiceRefundInfoPerson(String invoiceId) {
        InvoiceRefundDto paymentInvoiceDto = new InvoiceRefundDto();
        Invoice invoice = invoiceMapper.selectInvoiceByInvoiceId(invoiceId);
        BeanUtils.copyProperties(invoice, paymentInvoiceDto);
        PaymentRecord paymentRecord = paymentRecordMapper.selectPaymentRecordByPaymentRecordId(invoice.getPaymentRecordId());
        String[] split = paymentRecord.getRegistrationId().split(",");
        StringJoiner projectNames = new StringJoiner(";");
        StringJoiner classNames = new StringJoiner(";");
        StringJoiner feeNames = new StringJoiner(";");
        for (String registrationId : split) {
            RegistrationDto registrationDto = registrationMapper.selectRegistrationByRegistrationId(registrationId);
            TrainingClass trainingClass = trainingClassMapper.selectTrainingClassByTrainingClassId(registrationDto.getExampleThree());
            BeanUtils.copyProperties(registrationDto, paymentInvoiceDto);
            projectNames.add(registrationDto.getProjectMap());
            classNames.add(trainingClass.getTrainingClassName());
            feeNames.add(registrationDto.getProjectSum());
        }
        paymentInvoiceDto.setProjectMap(projectNames.toString());
        paymentInvoiceDto.setTrainingClass(classNames.toString());
        paymentInvoiceDto.setProjectSum(feeNames.toString());
        return paymentInvoiceDto;
    }
}
